package com.ny.service.impl;

import com.ny.dao.AdminDao;
import com.ny.dao.impl.AdminDaoImpl;
import com.ny.entity.Admin;
import com.ny.entity.Order;
import com.ny.entity.Product;
import com.ny.entity.Student;
import com.ny.service.AdminService;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

public class AdminServiceImpl implements AdminService {
    private final AdminDao adminDao = new AdminDaoImpl() {
        @Override
        public int updateAdmin(Admin manager) {
            return adminDao.updateAdmin(manager);
        }

        @Override
        public Admin getManagerById(int managerId) {
            return adminDao.getManagerById(managerId);
        }
    };

    @Override
    public Admin selectManager(String unername, String password) throws Exception {
        return adminDao.selectAdmin(unername, password);
    }

    @Override
    public Admin login(String username, String password) {
        try {
            return adminDao.selectAdmin(username, password);
        } catch (SQLException e) {
            e.printStackTrace();
            return null; // 或抛出业务异常
        }
    }

    @Override
    public List<Student> getAllStudents() {
        try {
            return adminDao.selectAllStudents();
        } catch (SQLException e) {
            e.printStackTrace();
            return Collections.emptyList(); // 或抛出业务异常
        }
    }

    @Override
    public int addStudent(Student student) {
        try {
            return adminDao.insertStudent(student);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0; // 或抛出业务异常
        }
    }

    @Override
    public int updateStudent(Student student) {
        try {
            return adminDao.updateStudent(student);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteStudent(int studentId) {
        try {
            return adminDao.deleteStudent(studentId);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int addProduct(Product product) {
        try {
            return adminDao.insertProduct(product);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateProduct(Product product) {
        try {
            return adminDao.updateProduct(product);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteProduct(int productId) {
        try {
            return adminDao.deleteProduct(productId);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Order> getAllOrders() {
        try {
            return adminDao.selectAllOrders();
        } catch (SQLException e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

    @Override
    public int deleteOrder(int orderId) {
        try {
            return adminDao.deleteOrder(orderId);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }
}